<?php
/**
* Display the summary of transactions debits
*
* @author       Fabrice Douteaud <admin@clearbudget.net>
* @package      snippets
* @access       public
*/

/***********************************************************************

  Copyright (C) 2008  Fabrice Douteaud (admin@clearbudget.net)

    This file is part of ClearBudget.

    ClearBudget is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    ClearBudget is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with ClearBudget.  If not, see <http://www.gnu.org/licenses/>.


************************************************************************/
//prevent direct access
if(!defined('ENGINEON')) die('Direct access forbidden');

?>
<div class="tableDiv">
<table>
<caption></caption>
<tr>
  <td>
    <img src="<?php echo $url3; ?>" alt="" border="1" style="width:500px;height:200px;border:1px" />
  </td>
  <td>
    <img src="<?php echo $url4; ?>" alt="" border="1" style="width:400px;height:150px;border:1px" />
  </td>
</tr>
</table>
<table class="tableReport">
<caption><?php echo $keys->tableTitle_debit; ?></caption>
<tr><th><?php echo $keys->text_Debit; ?></th>
<?php
foreach($dates as $date) {
  echo '<th class="nobg">'.$date.'</th>';
  }
?>
 <th><?php echo $keys->text_Total; ?></th>
 <th><?php echo $keys->text_Percent; ?></th>
 <th><?php echo $keys->text_Average; ?></th>
</tr>
<?php
$alt = '';
foreach($categories as $id=>$name) {
  // build the category link
  $cateoryLink = '<a href="#" action="listCategoryItems_categorySelected='.$id.'_debit=1_backAction=tableDebits">'.$report->getCategoryName($id).'</a>';
  // get the total for that category
  (isset($debitTotalPerCategory[$id]))? $total = $report->formatNumber($debitTotalPerCategory[$id]): $total = 'n/a';
  // if total=0 we skip this category
  if($total == 0) continue;
  // start the row
  echo '<tr><td class="category">'.$cateoryLink.'</td>';
  // loop over the column
  $count = 0;
  foreach($dates as $date) {
    $class = 'categoryAmount';
    // change color if amount is greater than max amount per month
    if($report->categoryMaxAmountPerMonth[$id] != null && $report->categoryMaxAmountPerMonth[$id] != '' 
      && $report->categoryMaxAmountPerMonth[$id]!= 0 && isset($debits[$id][$date])
      && $debits[$id][$date] > 0 && $debits[$id][$date] > $report->categoryMaxAmountPerMonth[$id]) {
      $class = 'categoryAmountOver';
      }
      
    // build the link or the n/a text
    if(isset($debits[$id][$date])) {
      echo '<td class="'.$class.'"><a href="#" action="listCategoryItems_monthSelected='.$date.'_categorySelected='.$id.'_debit=1_backAction=tableDebits">'.$report->formatNumber($debits[$id][$date]).'</a></td>';
      }
    else {
      echo'<td>n/a</td>';
      }
    $count ++;
    }
  
  // print the total and average
  echo '<td class="total">'.$total.'</td>';
  echo '<td>'.$debitsPctPerCategory[$id].'</td>';
  echo '<td>'.sprintf("%.2f",($total/$count)).'</td></tr>';
  }
 ?>
<tr><th><?php echo $keys->text_Total; ?></th>
<?php
foreach($dates as $date) {
  if(isset($totalDebitsPerMonth[$date])) echo '<th>'.$totalDebitsPerMonth[$date].'</th>';
  else echo '<th>'.$report->formatNumber(0).'</th>';
  }
?>
 <th><?php echo $totalDebits; ?></th>
 <th colspan="2"></th>
</tr>
</table>
</div>